# -*- coding: utf-8 -*-
"""
Created on Mon Mar 28 22:40:41 2016

@author: xuesonghu
"""

import socket
import struct
import GlobalSaveVariable as Glo
import binascii




s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('10.0.0.160',1000))

###信息端下发 0x20000001 模拟器返回 0x60000001
headerACK = Glo.HeaderAck()
headerACK.struMsgHeader = Glo.HANDSHAKEINQ
s.send(headerACK.pack())
recvdata = s.recv(1024)
headerACK.unpack(recvdata)
if headerACK.struMsgHeader == Glo.HANDSHAKEACK:
    print 'HANDSHAKEACK ACK'
else:
    print 'HANDSHAKEACK NAK'

###信息端下发 0x200000007 模拟器返回 0x60000007

headerACK.struMsgHeader = Glo.SELFTESTCTL
s.send(headerACK.pack())
recvdata = s.recv(1024)
headerACK.unpack(recvdata)
if headerACK.struMsgHeader == Glo.SELFTESTACK:
    print 'SELFTESTACK ACK'
else:
    print 'SELFTESTACK NAK'
### 配置信息
sysWorkTime = Glo.SysWorkTime()
sysWorkTime.struMsgHeader = Glo.INITPARA
s.send(sysWorkTime.pack())
recvdata = s.recv(1024)
headerACK.unpack(recvdata)
if headerACK.struMsgHeader == Glo.INITACK:
     print 'INITACK ACK'
else:
     print 'INITACK NAK'
while True:
    recvdata = s.recv(2048)
    if struct.unpack('I',recvdata[0:4])[0] == Glo.JUSTATE:
        justateData = Glo.JustateData()
        justateData.unpack(recvdata)
        print justateData.strNetTime.ucEpochNum,justateData.strNetTime.ucFrameNum,justateData.strNetTime.usSlotNum
    elif struct.unpack('I',recvdata[0:4])[0] == Glo.HWSTATE:
        hwState = Glo.HwState()
        hwState.unpack(recvdata)
        print ord(recvdata[0]),ord(recvdata[1]),ord(recvdata[2]),ord(recvdata[3]),ord(recvdata[4])


